using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CastleDefenceGame
{
    public partial class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Background background;
        Foreground foreground;
        Fort fort;
        List<Enemy> EnemyList = new List<Enemy>(); 
        int spawnTime = 0;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            graphics.PreferredBackBufferWidth = 900;
            graphics.PreferredBackBufferHeight = 600;
            graphics.IsFullScreen = false;

            background = new Background();
            foreground = new Foreground();
            fort = new Fort();

            base.Initialize();
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            background.LoadContent(Content, "Background");
            foreground.LoadContent(Content, "Foreground");
            fort.LoadContent(Content, "Fort1"); 
        }
        protected override void UnloadContent()
        {
        }
        protected override void Update(GameTime gameTime)
        {
            if (spawnTime > 300 && EnemyList.Count < 10)
            {
                SpawnEnemy(50, new Vector2(0, 270), 2, 10);
                spawnTime = 0;
            }
            foreach (var enemy in EnemyList)
            {
                enemy.Update(gameTime, fort);
            }
            background.Update(gameTime);
            foreground.Update(gameTime);
            fort.Update(gameTime);
            base.Update(gameTime);
            spawnTime++;
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            
            background.Draw(spriteBatch);
            foreground.Draw(spriteBatch);
            fort.Draw(spriteBatch);
            foreach (var enemy in EnemyList)
            {
                enemy.Draw(spriteBatch);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
